﻿using eDataBoard.basePage;
using Newtonsoft.Json.Linq;
using SqlSugar;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http; 
using System;
using OpcSelfHost.WebApi.model;
using Newtonsoft.Json;
using OPCDa;
using OpcSelfHost.basePage;

namespace OpcSelfHost.WebApi.controllers
{
    [RoutePrefix("api")]
    public class OpcController : ApiController
    {
        [HttpGet]
        [HttpPost]
        public bool start()
        {

            try
            {

                OPCHelper OPCHelper = new OPCHelper(CommonBase.serverip, CommonBase.servername, 1);
                if (OPCHelper.Connected)
                { 
                    OPCHelper.Dispose();
                    return true;
                }
                else
                {
                    return false;
                }
                 
            }
            catch (Exception ex)
            {

                return false;
            }
        }

        [HttpGet]
        [HttpPost]
        public ReturnMsg points()
        {

            string return_msg = "";
            object return_data = null;
            try
            {

                OPCHelper OPCHelper = new OPCHelper(CommonBase.serverip, CommonBase.servername, 1);
                if (OPCHelper.Connected)
                {
                     return_data = OPCHelper.getAllItems();
                     OPCHelper.Dispose();
                    return ReturnMsg.setReturnMsg(0, "成功", return_data);
                }
                else
                {
                    return ReturnMsg.setReturnMsg(1, "连接异常", return_data);
                }


            }
            catch (Exception ex)
            {
                return_msg = ex.Message;
                return ReturnMsg.setReturnMsg(1, return_msg, return_data);
            }

        }
       


        [HttpGet]
        [HttpPost]
        public ReturnMsg read(string tag)
        {
            object return_data = null;
            string return_msg = null;
            try
            {

                OPCHelper OPCHelper = new OPCHelper(CommonBase.serverip, CommonBase.servername, 1);
                if (OPCHelper.Connected)
                {
                    string[] tags = { tag };
                    OPCHelper.AddItems(tags);
                    Array values = OPCHelper.Read(tags);
                    return_data = values.GetValue(1);
                    OPCHelper.Dispose();
                    return ReturnMsg.setReturnMsg(0, return_msg, return_data);
                }
                else
                {
                    return ReturnMsg.setReturnMsg(1, "连接异常", return_data);
                }


            }
            catch (Exception ex)
            {
                return_msg = ex.Message;
                return ReturnMsg.setReturnMsg(1, return_msg, return_data);
            }

        }

        [HttpGet]
        [HttpPost]
        public ReturnMsg read(JObject jsonObj)
        {

            object return_data = null;
            string return_msg = null;
            JToken listtags = null;
            try
            {
                OPCHelper OPCHelper = new OPCHelper(CommonBase.serverip, CommonBase.servername, 1);
                if (OPCHelper.Connected)
                {


                    listtags = jsonObj.GetValue("tags");

                    //传入单个
                    if (listtags.Type == JTokenType.String)
                    {
                        string[] tags = { listtags.ToString() };
                        OPCHelper.AddItems(tags);
                        Array values = OPCHelper.Read(tags);
                        return_data = values.GetValue(1);
                        return_msg = "单个获取成功";
                    }
                    else if (listtags.Type == JTokenType.Array)
                    {

                        string[] tags = listtags.Select(s => s.Value<string>()).ToArray();

                        OPCHelper.AddItems(tags);
                        var goodTags = OPCHelper.itemNames.ToArray();
                        Array values = OPCHelper.Read(goodTags);

                        Dictionary<string, object> dic_return = new Dictionary<string, object>();

                        for (int i = 0; i < goodTags.Length; i++)
                        {
                            if (!dic_return.ContainsKey(goodTags[i]))
                            {
                                dic_return.Add(goodTags[i], values.GetValue(i + 1));
                            }
                            
                        }

                        return_data = dic_return;
                        return_msg = "批量获取成功";

                    }
                    else
                    {
                        return_data = null;
                        return_msg = "传入数据格式错误";

                    }


                    OPCHelper.Dispose();
                    return ReturnMsg.setReturnMsg(0, return_msg, return_data);

                }
                else
                {
                    return_msg = "连接异常";
                    return ReturnMsg.setReturnMsg(1, return_msg, return_data);
                }


            }
            catch (Exception ex)
            {
                return_msg = ex.Message;
                return ReturnMsg.setReturnMsg(1, return_msg, return_data);
            }




        }

        [HttpGet]
        [HttpPost]
        public ReturnMsg write(JObject jsonObj)
        {

            object return_data = null;
            string return_msg = null;
            JToken listtags = null;
            JToken listvalues = null;
            try
            {


                OPCHelper OPCHelper = new OPCHelper(CommonBase.serverip, CommonBase.servername, 1);
                if (OPCHelper.Connected)
                {
                    listtags = jsonObj.GetValue("tags");
                    listvalues = jsonObj.GetValue("values");

                    string[] tags = listtags.Select(s => s.Value<string>()).ToArray();
                    string[] values = listvalues.Select(s => s.Value<string>()).ToArray();


                    OPCHelper.AddItems(tags);


                    OPCHelper.AsyncWrite(tags, values);

                    return_data = tags;
                    return_msg = "批量写入成功";
                    OPCHelper.Dispose();
                    return ReturnMsg.setReturnMsg(0, return_msg, return_data);


                }
                else
                {

                    return ReturnMsg.setReturnMsg(1, "异常连接", return_data);

                }





            }
            catch (Exception ex)
            {
                return_msg = ex.Message;
                return ReturnMsg.setReturnMsg(1, return_msg, return_data);
            }




        }

    }
}
